﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Legazpi.Payroll.DataAccess.DataObjects;
using System.Configuration;
using Legazpi.Payroll.BusinessLogic.Services;

namespace Legazpi.Payroll.Web.Helpers
{
    public static class Helper
    {

        public static List<Supervisor> GetSupervisors()
        {
            int supervisorPositionId;
            if (int.TryParse(ConfigurationManager.AppSettings["SupervisorId"].ToString(), out supervisorPositionId))
            {
                var _employeeService =  new EmployeeService();

                return _employeeService.GetAll()
                    .Where(x =>  x.PositionId == supervisorPositionId)
                    .Select(x =>
                            new Supervisor
                            {
                                Id = x.Id,
                                Fullname = string.Format("{0}, {1}", x.LastName, x.FirstName)
                            }).ToList();                            
            }
            return new List<Supervisor>();
        }

        public static PayrollPeriodModel GetPayrollPeriod()
        {
            var payrollPeriod = new PayrollPeriodModel();
            var today = DateTime.Now;
            if (today.Day >= 1 && today.Day < 16)
            {
                payrollPeriod.StartDate = new DateTime(today.Year, today.Month, 1);
                payrollPeriod.EndDate = new DateTime(today.Year, today.Month, 15);
            }
            else 
            {
                payrollPeriod.StartDate = new DateTime(today.Year, today.Month, 16);
                payrollPeriod.EndDate = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year,today.Month));            
            }
            return payrollPeriod;
        }
    }

    public class Supervisor
    {
        public int Id { get; set; }
        public string Fullname { get; set; }
    }
    
    public class PayrollPeriodModel
    {
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }        
    }
}